#include "timer.h"
#include "stm32f10x_tim.h"
#include "msg_queue.h"
#include "timer_util.h"

extern MsgQueue ATMsgQueue;
extern MsgQueue TIMMsgQueue;


void initTimer(void)
{
	TIM_TimeBaseInitTypeDef config;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
	
	config.TIM_Period    = 100;
	config.TIM_Prescaler = 720;
	config.TIM_ClockDivision  = TIM_CKD_DIV4;
	config.TIM_CounterMode  = TIM_CounterMode_Up;
	config.TIM_RepetitionCounter  = 0;
	TIM_TimeBaseInit(TIM2, &config);
	
	TIM_ClearFlag(TIM2, TIM_FLAG_Update);
	
	TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
	TIM_Cmd(TIM2, ENABLE);	
}



void TIM2_IRQHandler(void)
{
	if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET){
		TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
		static uint32_t cnt  = 0;
		cnt++;
		if(cnt == 100){
			cnt  = 0;
			//PostMessage(&TIMMsgQueue, MSG_TIMER, REPORT, 0, 0);
			OnTimerTick();
		}	
	}

}